<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Company"%>
<%@page import="db.CompanyDAO"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	CompanyDAO companyDAO = (CompanyDAO)ctx.getBean("CompanyDAO");
	request.setCharacterEncoding("utf-8");
	String cid = request.getParameter("cid");
	int tid = (Integer)session.getAttribute("tid");
	int id = 0;
	if(cid!=null || !"".equals(cid)){
		id = Integer.parseInt(cid);
	}
	String message = "";
	
	String type = request.getParameter("type");
	//type=2提交表单
	if(type!=null && (type=="2" || "2".equalsIgnoreCase(type))){
		//System.out.println("提交");
		request.setAttribute("type",2);
		String name = request.getParameter("cname");
		String receiver1 = request.getParameter("receiver1");
		String receiver2 = request.getParameter("receiver2");
		String receiver3 = request.getParameter("receiver3");
		String receiver4 = request.getParameter("receiver4");
		String receiver5 = request.getParameter("receiver5");
		String phone = request.getParameter("phone");
		String identity = request.getParameter("identity");
		String remark = request.getParameter("remark");
		
		Company company = new Company();
		company = companyDAO.findById(id);
		//判断登录名是否为空，是否重复
		if(name==null || "".equals(name)){
			message = "修改施工单位失败，施工单位名称为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/coninfo/mod_coninfo.vm");
			return;
		}else{
			if((name == company.getName() || name.equals(company.getName())) && tid==company.getTeamId() && 0==company.getIsDelete()){
				message = "施工单位名称未做修改！";
			}else{
				List md = companyDAO.findNameAndTeamId(name,tid);
				if(md != null && md.size() > 0 ){
					message = "修改施工单位失败，施工单位名称已经存在。";
					request.setAttribute("suess",false);
					request.setAttribute("message",message);
					pageContext.forward("/vm/coninfo/mod_coninfo.vm");
					return;
				}
			}
		}
		
		if(receiver1==null){
			receiver1="";
		}
		if(receiver2==null){
			receiver2="";
		}
		if(receiver3==null){
			receiver3="";
		}
		if(receiver4==null){
			receiver4="";
		}
		if(receiver5==null){
			receiver5="";
		}
		if(phone==null){
			phone="";
		}
		if(identity==null){
			identity="";
		}
		if(remark==null){
			remark="";
		}
		company.setIdentity(identity);
		company.setName(name);
		company.setPhone(phone);
		company.setReceiver1(receiver1);
		company.setReceiver2(receiver2);
		company.setReceiver3(receiver3);
		company.setReceiver4(receiver4);
		company.setReceiver5(receiver5);
		company.setRemark(remark);
		
		companyDAO.attachDirty(company);
		
		//System.out.println(uid);
		message = "恭喜您！修改施工单位成功！";
		request.setAttribute("message",message);
		request.setAttribute("suess",true);
		request.setAttribute("cid",id);
		pageContext.forward("/vm/coninfo/mod_coninfo.vm");
		return;
		
		
	}else{
	//正常显示
		//System.out.println("显示");
		Company company = new Company();
		company = companyDAO.findById(id);
		
		request.setAttribute("company",company);
		request.setAttribute("type",1);
		request.setAttribute("cid",id);
		pageContext.forward("/vm/coninfo/mod_coninfo.vm");
		
	}
	
%>
